/**
 * Created by Administrator on 2016/10/11.
 */
$(function(){
    var box=$("#box ul");
    var oLi=$("#box ul li");
    var leftBtn=$("#left");
    var rightBtn=$("#right");
    var num=0;

    box.html+=box.html();
    box.css({"width":oLi.length*oLi[0].offsetWidth});
    var width=box.innerWidth();
    var imgWidth=oLi[0].offsetWidth;


    $(window).resize(function(){
        width=box.innerWidth();
    });
    //console.log(imgWidth);
    var index=0;
    var timer=setInterval(autoRun,3000);
    box.mouseenter(function(){
        clearInterval(timer);
    });
    box.mouseleave(function(){
        clearInterval(timer);
        timer=setInterval(autoRun,3000);
    });
    leftBtn.on("click",function(){
        clearInterval(timer);
        sliderLeft();
    });
    rightBtn.on("click",function(){
        clearInterval(timer);
        sliderRight();
    });

    function autoRun(){
        if(num>=4)
        {
            //alert(1);
            num=0;//当图片移动到来末尾重复的图片时，让left等与0
            /* if(index==0)
             {
             box.css({"left":"-imgWidth"});
             index=1;//当图片移动到第一个重复的图片时，让left等于-1200
             }*/
        }

        $("#box").css("left",imgWidth*num*(-1)+"px");
        //console.log(imgWidth*num*(-1));
        num++;
    }
    function sliderLeft(){
        num--;
        $(window).resize(function(){
            width=box.innerWidth();
        });
        if(num<=-1){
            num=3;
        }
        //document.title=num;
        $("#box").css("left",imgWidth*num*(-1)+"px");
    }
    function sliderRight(){
        num++;
        $(window).resize(function(){
            width=box.innerWidth();
        });
        if(num>=4){
            num=0
        }
        //document.title=num;
        $("#box").css("left",imgWidth*num*(-1)+"px");
    }
    /*******************************以下是当页面切换时，停止或开始轮播计时器**************************************/
    window.onblur = blurMe;
    window.onfocus = focusMe;

    function blurMe() {
        clearInterval(timer);
    }
    function focusMe() {
        timer=setInterval(autoRun,3000);
    }

});